<!--商品类型页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">类型列表</span>
    <el-button type="primary" style="float:right;margin-top:13px;"
               @click="beforeAddType">添加类型</el-button>
  </div>
  <!-- 添加类型弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;"
             :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
          <el-form-item label="类型名称">
            <el-input placeholder="请输入类型名称" v-model="saveTypeForm.typename"></el-input>
          </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveType">确定</el-button>
    </template>
  </el-dialog>

  <!-- 商品类型列表 搜索卡片 -->
  <el-card style="margin: 20px;">
    <el-form style="padding-top:10px;">
      <el-row :gutter="30">
        <el-col :span="8">
          <el-form-item label="类型名称">
            <el-input placeholder="请输入要搜索的类型名称" v-model="searchTypeForm.typename"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="类型ID">
            <el-input placeholder="请输入要搜索的类型ID" v-model="searchTypeForm.id"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item>
            <el-button @click="resetSearch">重置</el-button>
            <el-button type="primary" @click="loadType">查询</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-card>
  <!-- 用车申请表格 -->
  <el-card style="margin:20px">
    <el-table :data="typeArr">
      <el-table-column type="index" width="80" align="center" prop="id" label="编号"></el-table-column>
      <el-table-column align="center" prop="id" label="类型ID"></el-table-column>
      <el-table-column align="center" prop="typename" label="类型名称"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editType(scope.row.id)">编辑</el-button>
          <el-button link type="primary" size="small" @click="deleteType(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

  </el-card>
</template>
<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";
//定义对象用来保存类型搜索条件
const searchTypeForm = ref({id: '', typename: ''});
//定义数组用来保存商品类型列表表格数据
const typeArr = ref([]);

//定义加载类型列表的方法
const loadType = () => {
  console.log(searchTypeForm.value);
  let data = qs.stringify(searchTypeForm.value)
  axios.get(BASE_URL + '/v1/types/select?' + data).then((response) => {
    if (response.data.code == 2000) {
      typeArr.value = response.data.data;
      console.log(typeArr.value);
    } else {
      ElMessage.error(response.data.msg)
    }
  })
}
//页面加载完毕立即调用加载用户的方法
onMounted(() => {
  loadType();
})
//定义重置搜索的方法
const resetSearch = () => {
  searchTypeForm.value = {};//清空搜索条件
  loadType();//重新加载所有用户数据
}

//定义变量控制创建申请单弹窗是否出现
const dialogVisible = ref(false);
//定义变量控制dialog的标题
const dialogTitle = ref('添加类型');

//定义添加类型的方法
const beforeAddType = () => {
  dialogTitle.value = '添加类型';
  dialogVisible.value = true;
}

//定义对象用来收集添加类型表单的数据
const saveTypeForm = ref({typename: ''});
const saveType = () => {
  console.log(saveTypeForm.value);
  let data = qs.stringify(saveTypeForm.value);
  axios.post(BASE_URL + '/v1/types/save', data).then((response) => {
    if (response.data.code == 2000) {
      ElMessage.success('保存成功!');
      saveTypeForm.value = {};//保存成功后要清空弹窗表单并关闭弹窗
      dialogVisible.value = false;
      //新增成功后应该重新刷新页面
      loadType();
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}

//定义编辑商品类型的方法
const editType = (id) => {
  dialogVisible.value = true;
  dialogTitle.value = '编辑维修车辆';
  axios.get(BASE_URL + "/v1/types/select?id=" + id).then((response) => {
    if (response.data.code == 2000) {
      saveTypeForm.value = response.data.data[0];
      console.log("回显的信息:" + saveTypeForm.value);
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}

//定义处理弹窗关闭的方法
const handleClose = () => {
  if (confirm('您确认要关闭弹窗吗?')) {
    dialogVisible.value = false;
  }
}

//定义删除类型方法
const deleteType = (id) => {
  if (confirm('您确认要删除该类型吗?')) {
    axios.post(BASE_URL + '/v1/types/delete/typeId/' + id).then((response) => {
      if (response.data.code == 2000) {
        ElMessage.success('删除成功!');
        loadType();
      } else {
        ElMessage.error(response.data.msg);
      }
    })
  }
}

</script>

<style scoped>
</style>